import message from "./message.js";
import store from './store.js';


let isCloud = false;
// let baseURL = "http://localhost:8010/wx";

let baseURL = "https://info.aresfilm.cn:8010/wx";


if (isCloud) {
	baseURL = "/container-fax/fax";
	var c1 = new wx.cloud.Cloud({
		resourceEnv: 'kerqinserver-5gyuxi1ecf8c0a66'
	});
	c1.init();
}

const httpObject = {
	login: function(code) {
		return httpObject.get({
			url: "/login/jscode",
			param: {
				code
			}
		}).then(data => {
			store.commit("loginSuccess", data);
			uni.hideLoading();
		});
	},
	_changeParam: param => {
		param.data = param.param;
		param.url = baseURL+param.url;
		return param;
	},
	put: param => {
		param.method = "PUT";
		return httpObject._request(httpObject._changeParam(param));
	},
	get: param => {
		param.method = "GET";
		return httpObject._request(httpObject._changeParam(param));
	},
	post: param => {
		param.method = "POST";
		return httpObject._request(httpObject._changeParam(param));
	},
	"delete": param => {
		param.method = "DELETE";
		return httpObject._request(httpObject._changeParam(param));
	},

	_request: param => {
		return new Promise((resolve, reject) => {
			if (isCloud) {} else {
				let localHeader = {
					'content-type': 'application/x-www-form-urlencoded',
				};
				
				if (store.state.sessionId) {
					localHeader["Cookie"] = "JSESSIONID="+store.state.sessionId;
				}
				
				let requestOption = {
				    url: param.url,
					method: param.method,
				    data: param.data,
					withCredentials: true,
					header: localHeader,
					fail: function(f) {
						uni.hideLoading();
						console.log("连接服务器失败！");
						message.error("连接服务器失败！");
					},
					error: function(e) {
						uni.hideLoading();
						console.log("网络错误，无法连接到服务器！");
						message.error("网络错误，无法连接到服务器！");
					},
				    success: function(res) {
						let cookieSet = res.header['Set-Cookie'];
						
						if (cookieSet) {
							let jSessionId = cookieSet.split(";")[0].split("=")[1];
							
							if (jSessionId) {
								store.commit("setSessionId", jSessionId);
							}
						}
						
				        let response = res.data;
				        if (response.STATUS === "SUCCESS") {
				        	resolve(response.DATA);
							uni.hideLoading();
				        } else if (response.STATUS === "FAIL") {
							uni.hideLoading();
				        	message.warning(response.MESSAGE);
				        } else if (response.STATUS === "LOGIN_FAIL") {
							uni.hideLoading();
							store.commit("setRequestOption", requestOption);
							
				        	uni.login({
				        		success: function(res) {
				        			httpObject.login(res.code);
				        		}
				        	});
				        } else if (response.STATUS === "BIND_FAIL") {
							let openId = response.MESSAGE;
							store.commit("saveOpenId", openId);
							
							httpObject.put({
								url: "/autor-bind",
								param: {
									openId
								}
							}).then(function(res) {
								store.commit("loginSuccess", res);
								uni.hideLoading();
							});
							
							
				        } else if (response.STATUS === "ERROR") {
							uni.hideLoading();
				        	message.error(response.MESSAGE);
				        } else {
							uni.hideLoading();
							console.log("weizhicuowu")
				        	message.error("系统出现未知错误！");
				        }
				    }
				};
				uni.hideLoading();
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				uni.request(requestOption);
			}
			
		});
	}
}

export default httpObject;